import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress';
import Cookies from 'js-cookie';
import axios from 'axios';

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [
  {
    path: '/',
    component: () => import('@/views/index/index.vue')
  },
  {
    path: '/article/add',
    component: () => import('@/views/article/add.vue'),
  },
  {
    path: '/article/category_add',
    component: () => import('@/views/article/category_add.vue'),
  },
  {
    path: '/article/category_lst',
    component: () => import('@/views/article/category_lst.vue'),
  },
  {
    path: '/article/category_mod',
    component: () => import('@/views/article/category_mod.vue'),
  },
  {
    path: '/article/del',
    component: () => import('@/views/article/del.vue'),
  },
  {
    path: '/article/lst',
    component: () => import('@/views/article/lst.vue'),
  },
  {
    path: '/article/mod',
    component: () => import('@/views/article/mod.vue'),
  },
  {
    path: '/article/preview',
    component: () => import('@/views/article/preview.vue'),
  },
  {
    path: '/home/index',
    component: () => import('@/views/home/index.vue'),
  },
  {
    path: '/home/logout',
    component: () => import('@/views/home/logout.vue'),
  },
  {
    path: '/index/index',
    component: () => import('@/views/index/index.vue'),
  },
  {
    path: '/member/add',
    component: () => import('@/views/member/add.vue'),
  },
  {
    path: '/member/lst',
    component: () => import('@/views/member/lst.vue'),
  },
  {
    path: '/member/mod',
    component: () => import('@/views/member/mod.vue'),
  },
  {
    path: '/member/preview',
    component: () => import('@/views/member/preview.vue'),
  },
  {
    path: '/msg',
    component: () => import('@/views/msg.vue'),
  },
  {
    path: '/safe/change_password',
    component: () => import('@/views/safe/change_password.vue'),
  },
  {
    path: '/welcome/login',
    component: () => import('@/views/welcome/login.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局路由前置守卫
router.beforeEach(async (to, from, next) => {
  if (to.path != from.path) {
    NProgress.start();

    let [controller, action] = to.path.substring(1).split('/');
    if (!controller) controller = 'index';
    if (!action) action = 'index';
    if (['welcome', 'index', 'msg'].indexOf(controller) > -1) {
      next();
      return;
    }

    var auth = Cookies.get("auth");
    var userid = Cookies.get("userid");
    var username = Cookies.get("username");
    var groupid = Cookies.get("groupid");
    var power = Cookies.get("power");

    if (!parseInt(userid)) {
      router.replace({
        path: '/welcome/login',
      });
      return;
    }
    if (parseInt(groupid) == 1) {
      next();
      return;
    }


    //免验证
    if (['home', 'safe'].indexOf(controller) > -1) {
      next();
      return;
    }
    //去服务端验证
    let data = await axios.post('/api/auth/check', {
      userid,
      controller,
      action
    });
    if (!parseInt(data.code)) {
      router.push({
        path: '/msg',
        query: { code: 403 },
      })
      return;
    }


    next();
  }
});

// 全局路由后置守卫
router.afterEach((to, from) => {
  NProgress.done();

})


export default router
